/* 
Set directory 
*/

    global root ENTER DIRECTORY
                                
	global aser 	$root\Analysis\Dta\ASER
	
	global nrega 	$root\Analysis\Dta\NREGA
	
	global output 	ENTER DIRECTORY
	
	
set more off
use "$aser\aser_temp_analysis_ready", clear


label var nmath "Math Score (in SD)"
label var nread "Read Score (in SD)"

label var ll1ktotlbinlo "PY GS Days $<$15C"
label var ll1ktotlbinup "PY GS Days $>$21C"

label var ll1nktotlbinlo "PY NGS Days $<$15C"
label var ll1nktotlbinup "PY NGS Days $>$21C"

label var ll1totlbinlo "PY Days $<$15C"
label var ll1totlbinup "PY Days $>$21C"

label var totlbinlo "CY Days $<$15C"
label var totlbinup "CY Days $>$21C"

label var ff1totlbinlo "NY Days $<$15C"
label var ff1totlbinup "NY Days $>$21C"

label var ll1tot_ldeg_days_lo21 "DD $<$21C"
label var ll1tot_ldeg_days_up21 "DD $>$21C"

label var ll1lbin1 "PY Days $<$13C"
label var ll1lbin2 "PY Days 13-15C"
label var ll1lbin3 "PY Days 15-17C"
label var ll1lbin4 "PY Days 17-19C"
label var ll1lbin5 "PY Days 19-21C"
label var ll1lbin6 "PY Days 21-23C"
label var ll1lbin7 "PY Days 23-25C"
label var ll1lbin8 "PY Days 25-27C"
label var ll1lbin9 "PY Days 27-29C"
label var ll1lbin10 "PY Days $>$29C"

label var ll1klbin1 "PY GS Days $<$13C"
label var ll1klbin2 "PY GS Days 13-15C"
label var ll1klbin3 "PY GS Days 15-17C"
label var ll1klbin4 "PY GS Days 17-19C"
label var ll1klbin5 "PY GS Days 19-21C"
label var ll1klbin6 "PY GS Days 21-23C"
label var ll1klbin7 "PY GS Days 23-25C"
label var ll1klbin8 "PY GS Days 25-27C"
label var ll1klbin9 "PY GS Days 27-29C"
label var ll1klbin10 "PY GS Days $>$29C"

label var ll1nklbin1 "PY NGS Days $<$13C"
label var ll1nklbin2 "PY NGS Days 13-15C"
label var ll1nklbin3 "PY NGS Days 15-17C"
label var ll1nklbin4 "PY NGS Days 17-19C"
label var ll1nklbin5 "PY NGS Days 19-21C"
label var ll1nklbin6 "PY NGS Days 21-23C"
label var ll1nklbin7 "PY NGS Days 23-25C"
label var ll1nklbin8 "PY NGS Days 25-27C"
label var ll1nklbin9 "PY NGS Days 27-29C"
label var ll1nklbin10 "PY NGS Days $>$29C"

label var ll1slbin1 "PY SY Days $<$13C"
label var ll1slbin2 "PY SY Days 13-15C"
label var ll1slbin3 "PY SY Days 15-17C"
label var ll1slbin4 "PY SY Days 17-19C"
label var ll1slbin5 "PY SY Days 19-21C"
label var ll1slbin6 "PY SY Days 21-23C"
label var ll1slbin7 "PY SY Days 23-25C"
label var ll1slbin8 "PY SY Days 25-27C"
label var ll1slbin9 "PY SY Days 27-29C"
label var ll1slbin10 "PY SY Days $>$29C"

label var ll1nslbin1 "PY NSY Days $<$13C"
label var ll1nslbin2 "PY NSY Days 13-15C"
label var ll1nslbin3 "PY NSY Days 15-17C"
label var ll1nslbin4 "PY NSY Days 17-19C"
label var ll1nslbin5 "PY NSY Days 19-21C"
label var ll1nslbin6 "PY NSY Days 21-23C"
label var ll1nslbin7 "PY NSY Days 23-25C"
label var ll1nslbin8 "PY NSY Days 25-27C"
label var ll1nslbin9 "PY NSY Days 27-29C"
label var ll1nslbin10 "PY NSY Days $>$29C"


label var childage "Age"

tab ll1lrain, gen(ll1lrain)
tab ll1krain, gen(ll1krain)
tab ll1nkrain, gen(ll1nkrain)

tab ll1srain, gen(ll1srain)
tab ll1nsrain, gen(ll1nsrain)

tab lrain, gen(lrain)
tab ff1lrain, gen(ff1lrain)

   
*Figure 4

areg nmath ll1klbin1 ll1klbin2 ll1klbin4 ll1klbin5 ll1klbin6 ll1klbin7 ll1klbin8 ll1klbin9 ll1klbin10 ll1nklbin1 ll1nklbin2 ll1nklbin4 ll1nklbin5 ll1nklbin6 ll1nklbin7 ll1nklbin8 ll1nklbin9 ll1nklbin10 ll1nkrain1 ll1nkrain3 ll1krain1 ll1krain3 l1khumid1 l1khumid4 l1nkhumid1 l1nkhumid4 i.year i.childage , absorb(gdistid) cluster(gdistid)

foreach num of numlist 1 2 4 5 6 7 8 9 10{
gen binkcoef`num' = _b[ll1klbin`num']
gen binkcoefcil`num' = _b[ll1klbin`num'] - 1.96*_se[ll1klbin`num']
gen binkcoefciu`num' = _b[ll1klbin`num'] + 1.96*_se[ll1klbin`num']
}

foreach num of numlist 1 2 4 5 6 7 8 9 10{
gen binnkcoef`num' = _b[ll1nklbin`num']
gen binnkcoefcil`num' = _b[ll1nklbin`num'] - 1.96*_se[ll1nklbin`num']
gen binnkcoefciu`num' = _b[ll1nklbin`num'] + 1.96*_se[ll1nklbin`num']
}

gen binkcoef3 = 0
gen binkcoefcil3 = 0
gen binkcoefciu3 = 0

gen binnkcoef3 = 0
gen binnkcoefcil3 = 0
gen binnkcoefciu3 = 0

preserve

keep binnkcoef* binnkcoefcil* binnkcoefciu* binkcoef* binkcoefcil* binkcoefciu*
gen id = _n

keep if id == 1

reshape long binnkcoef binnkcoefcil binnkcoefciu binkcoef binkcoefcil binkcoefciu, i(id) j(ll1lbin)

label var binnkcoefcil "95% CI"
label var binnkcoefciu "95% CI"
label var binkcoefcil "95% CI"
label var binkcoefciu "95% CI"

label var ll1lbin "Daily Average Temperature (C)"

twoway (line binkcoefcil binkcoefciu ll1lbin, astyle(ci ci) yline(0, lcolor(black)) mcolor(red red) lpattern(longdash longdash) lcolor(red red) sort) (connected binkcoef ll1lbin, sort msymbol(D) lcolor(red) mcolor(red) ) ///
(line binnkcoefcil binnkcoefciu ll1lbin, astyle(ci ci) yline(0, lcolor(black)) mcolor(blue blue) lpattern(shortdash shortdash) lcolor(blue blue) sort) (connected binnkcoef ll1lbin, sort msymbol(O) lcolor(blue) mcolor(blue)) ///
, graphregion(color(white)) plotregion(icolor(white)) ///
legend(order (6 5 3 2)  label(6 "NGS Estimate") label(5 "NGS 95% CI") label(3 "GS Estimate") label(2 "GS 95% CI")) ytitle("Math Score (in SD)") ///
xlabel(1 "<13C" 2 "13-15C" 3 "15-17C" 4 "17-19C" 5 "19-21C" 6 "21-23C" 7 "23-25C" 8 "25-27C" 9 "27-29C" 10 ">29C") ylabel(-0.015(0.005)0.005,angle (30))   
graph export "$output/F4a.png", replace width(800) height(600)		

restore

drop binnkcoef* binkcoef*

areg nread ll1klbin1 ll1klbin2 ll1klbin4 ll1klbin5 ll1klbin6 ll1klbin7 ll1klbin8 ll1klbin9 ll1klbin10 ll1nklbin1 ll1nklbin2 ll1nklbin4 ll1nklbin5 ll1nklbin6 ll1nklbin7 ll1nklbin8 ll1nklbin9 ll1nklbin10 ll1nkrain1 ll1nkrain3 ll1krain1 ll1krain3 l1khumid1 l1khumid4 l1nkhumid1 l1nkhumid4 i.year i.childage , absorb(gdistid) cluster(gdistid)

foreach num of numlist 1 2 4 5 6 7 8 9 10{
gen binkcoef`num' = _b[ll1klbin`num']
gen binkcoefcil`num' = _b[ll1klbin`num'] - 1.96*_se[ll1klbin`num']
gen binkcoefciu`num' = _b[ll1klbin`num'] + 1.96*_se[ll1klbin`num']
}

foreach num of numlist 1 2 4 5 6 7 8 9 10{
gen binnkcoef`num' = _b[ll1nklbin`num']
gen binnkcoefcil`num' = _b[ll1nklbin`num'] - 1.96*_se[ll1nklbin`num']
gen binnkcoefciu`num' = _b[ll1nklbin`num'] + 1.96*_se[ll1nklbin`num']
}

gen binkcoef3 = 0
gen binkcoefcil3 = 0
gen binkcoefciu3 = 0

gen binnkcoef3 = 0
gen binnkcoefcil3 = 0
gen binnkcoefciu3 = 0

preserve

keep binnkcoef* binnkcoefcil* binnkcoefciu* binkcoef* binkcoefcil* binkcoefciu*
gen id = _n

keep if id == 1

reshape long binnkcoef binnkcoefcil binnkcoefciu binkcoef binkcoefcil binkcoefciu, i(id) j(ll1lbin)

label var binnkcoefcil "95% CI"
label var binnkcoefciu "95% CI"
label var binkcoefcil "95% CI"
label var binkcoefciu "95% CI"

label var ll1lbin "Daily Average Temperature (C)"

twoway (line binkcoefcil binkcoefciu ll1lbin, astyle(ci ci) yline(0, lcolor(black)) mcolor(red red) lpattern(longdash longdash) lcolor(red red) sort) (connected binkcoef ll1lbin, sort msymbol(D) lcolor(red) mcolor(red) ) ///
(line binnkcoefcil binnkcoefciu ll1lbin, astyle(ci ci) yline(0, lcolor(black)) mcolor(blue blue) lpattern(shortdash shortdash) lcolor(blue blue) sort) (connected binnkcoef ll1lbin, sort msymbol(O) lcolor(blue) mcolor(blue)) ///
, graphregion(color(white)) plotregion(icolor(white)) ///
legend(order (6 5 3 2)  label(6 "NGS Estimate") label(5 "NGS 95% CI") label(3 "GS Estimate") label(2 "GS 95% CI")) ytitle("Read Score (in SD)") ///
xlabel(1 "<13C" 2 "13-15C" 3 "15-17C" 4 "17-19C" 5 "19-21C" 6 "21-23C" 7 "23-25C" 8 "25-27C" 9 "27-29C" 10 ">29C") ylabel(-0.015(0.005)0.005,angle (30))   
graph export "$output/F4b.png", replace width(800) height(600)		

restore

drop binnkcoef* binkcoef*

		   
*Table 1
restore
set more off			
local tablename "T1"
local table=1
local col=1
estimates clear

areg nmath ll1totlbinlo ll1totlbinup ll1lrain1 ll1lrain3 l1lhumid1 l1lhumid4 i.year i.childage , absorb(gdistid) cluster(gdistid)
estimates store cT`table'C`col++'
areg nmath ll1lbin1 ll1lbin2 ll1lbin4 ll1lbin5 ll1lbin6 ll1lbin7 ll1lbin8 ll1lbin9 ll1lbin10 ll1lrain1 ll1lrain3 l1lhumid1 l1lhumid4 i.year i.childage , absorb(gdistid) cluster(gdistid)
estimates store cT`table'C`col++'


areg nread ll1totlbinlo ll1totlbinup ll1lrain1 ll1lrain3 l1lhumid1 l1lhumid4  i.year i.childage , absorb(gdistid) cluster(gdistid)
estimates store cT`table'C`col++'
areg nread ll1lbin1 ll1lbin2 ll1lbin4 ll1lbin5 ll1lbin6 ll1lbin7 ll1lbin8 ll1lbin9 ll1lbin10 ll1lrain1 ll1lrain3 l1lhumid1 l1lhumid4  i.year i.childage , absorb(gdistid) cluster(gdistid)
estimates store cT`table'C`col++'


esttab cT`table'C* using "$output/`tablename'.tex", replace f ///
               label booktabs b(3) p(3) eqlabels(none) alignment(S) collabels("\multicolumn{1}{c}{$\beta$ / SE}") ///
               keep (ll1totlbinlo ll1totlbinup ll1lbin1 ll1lbin2 ll1lbin4 ll1lbin5 ll1lbin6 ll1lbin7 ll1lbin8 ll1lbin9 ll1lbin10, relax) ///
			   star(* 0.10 ** 0.05 *** 0.01) ///
               cells("b(fmt(4)star)" "se(fmt(4)par)") ///
			   stats(N r2, fmt(0 3) layout("\multicolumn{1}{c}{@}") ///
			   labels(`"Observations"' `"\(R^{2}\)"'))

			   

*Table 2
local tablename "T2"
local table=1
local col=1
estimates clear

areg nmath ll1totlbinlo ll1totlbinup totlbinlo totlbinup ff1totlbinlo ff1totlbinup ll1lrain1 ll1lrain3 l1lhumid1 l1lhumid4 lrain1 lrain3 lhumid1 lhumid4 ff1lrain1 ff1lrain3 f1lhumid1 f1lhumid4 i.year i.childage , absorb(gdistid) cluster(gdistid)
estimates store cT`table'C`col++'

areg nread ll1totlbinlo ll1totlbinup totlbinlo totlbinup ff1totlbinlo ff1totlbinup ll1lrain1 ll1lrain3 l1lhumid1 l1lhumid4 lrain1 lrain3 lhumid1 lhumid4 ff1lrain1 ff1lrain3 f1lhumid1 f1lhumid4 i.year i.childage , absorb(gdistid) cluster(gdistid)
estimates store cT`table'C`col++'


esttab cT`table'C* using "$output/`tablename'.tex", replace f ///
               label booktabs b(3) p(3) eqlabels(none) alignment(S) collabels("\multicolumn{1}{c}{$\beta$ / SE}") ///
               keep (ll1totlbinlo ll1totlbinup totlbinlo totlbinup ff1totlbinlo ff1totlbinup, relax) ///
			   star(* 0.10 ** 0.05 *** 0.01) ///
               cells("b(fmt(4)star)" "se(fmt(4)par)") ///
			   stats(N r2, fmt(0 3) layout("\multicolumn{1}{c}{@}") ///
			   labels(`"Observations"' `"\(R^{2}\)"'))				   
			   
			   
			   
			   
*Figure 5		   
xtile decile = avg_district_temp, nq(10)
label var decile "Climate Deciles"

bys decile: egen dec_avg_district_temp = max(avg_district_temp)
label var dec_avg_district_temp "Avg. Temp. (C)"

*Figure 5b
set more off
preserve
foreach num of numlist 1/10 {
areg nmath ll1ktotlbinlo ll1ktotlbinup ll1nktotlbinlo ll1nktotlbinup ll1krain1 ll1krain3 ll1nkrain1 ll1nkrain3 l1khumid1 l1khumid4 l1nkhumid1 l1nkhumid4 i.year i.childage if decile == `num', absorb(gdistid) cluster(gdistid)	
gen age`num' = _b[ll1ktotlbinup]
gen agecil`num' = _b[ll1ktotlbinup] - 1.96*_se[ll1ktotlbinup]
gen ageciu`num' = _b[ll1ktotlbinup] + 1.96*_se[ll1ktotlbinup]

}

gen age = .
gen agecil = .
gen ageciu = .

foreach num of numlist 1/10{
replace age = age`num' if decile == `num'
replace agecil = agecil`num' if decile == `num'
replace ageciu = ageciu`num' if decile == `num'
}

bys decile: keep if _n == 1

twoway (connected agecil ageciu decile, astyle(ci ci) yaxis(1)  ytitle("Math Score (SD)") ylabel(-0.02(0.01)0.01, angle (30)) lpattern(dash dash) lcolor(gray gray) mcolor(gray gray) sort)  ///
(connected age decile, sort msymbol(O) yaxis(1)  lcolor(red) mcolor(red))  ///
(bar dec_avg_district_temp decile, sort  yaxis(2) yline(24.67, lcolor(black) axis(2)) lcolor(gs14) color(none)), graphregion(color(white)) plotregion(icolor(white)) ///
legend(order (4 3 1)  label(4 "Average Temperature (C)") label(3 "Estimated Coefficient") label(1 "95% Confidence Interval")) ///
xlabel(1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "6" 7 "7" 8 "8" 9 "9" 10 "10")    
graph export "$output/F5b.png", replace width(800) height(600)		
restore	


*Figure 5c
preserve
set more off
foreach num of numlist 1/10 {
areg nread ll1ktotlbinlo ll1ktotlbinup ll1nktotlbinlo ll1nktotlbinup ll1krain1 ll1krain3 ll1nkrain1 ll1nkrain3 l1khumid1 l1khumid4 l1nkhumid1 l1nkhumid4 i.year i.childage if decile == `num', absorb(gdistid) cluster(gdistid)	
gen age`num' = _b[ll1ktotlbinup]
gen agecil`num' = _b[ll1ktotlbinup] - 1.96*_se[ll1ktotlbinup]
gen ageciu`num' = _b[ll1ktotlbinup] + 1.96*_se[ll1ktotlbinup]

}

gen age = .
gen agecil = .
gen ageciu = .

foreach num of numlist 1/10{
replace age = age`num' if decile == `num'
replace agecil = agecil`num' if decile == `num'
replace ageciu = ageciu`num' if decile == `num'
}

bys decile: keep if _n == 1

twoway (connected agecil ageciu decile, astyle(ci ci) yaxis(1)  ytitle("Read Score (SD)") ylabel(-0.02(0.01)0.01, angle (30)) lpattern(dash dash) lcolor(gray gray) mcolor(gray gray) sort)  ///
(connected age decile, sort msymbol(O) yaxis(1)  lcolor(red) mcolor(red))  ///
(bar dec_avg_district_temp decile, sort  yaxis(2) yline(24.27, lcolor(black) axis(2)) lcolor(gs14) color(none)), graphregion(color(white)) plotregion(icolor(white)) ///
legend(order (4 3 1)  label(4 "Average Temperature (C)") label(3 "Estimated Coefficient") label(1 "95% Confidence Interval")) ///
xlabel(1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "6" 7 "7" 8 "8" 9 "9" 10 "10")    
graph export "$output/F5c.png", replace width(800) height(600)		
restore	



*NREGA
foreach var of varlist gisdistrictname gisstatename{
gen Z=lower(`var')
drop `var'
rename Z `var'
}

rename gisdistrictname district
rename gisstatename state


merge m:1 state district using "$nrega\nrega_oth_clean.dta"

drop if _merge !=3

gen phase = 1 if phase1_act == 1
replace phase = 2 if phase2_act == 1
replace phase = 3 if phase == .

gen nrega_year = 2006 if phase == 1
replace nrega_year = 2007 if phase == 2
replace nrega_year = 2008 if phase == 3

gen nrega = 1 if phase == 1 & year >=2006
replace nrega = 1 if phase == 2 & year >= 2007
replace nrega = 1 if phase == 3 & year >=2008
replace nrega = 0 if nrega == .

gen lnrega = 1 if phase == 1 & year >=2007
replace lnrega = 1 if phase == 2 & year >=2008
replace lnrega = 1 if phase == 3 & year >=2009
replace lnrega = 0 if lnrega == .

gen lnrega_year = 2007 if phase == 1
replace lnrega_year = 2008 if phase == 2
replace lnrega_year = 2009 if phase == 3

gen t = .
replace t =  year - lnrega_year
label var t "Time"
tabulate t, gen(t_)


foreach var of varlist t_1 t_2 t_4 t_5 t_6 t_7 t_8 t_9 t_10 t_11 {
gen `var'_ll1totlbinlo = `var'*ll1totlbinlo
gen `var'_ll1totlbinup = `var'*ll1totlbinup
}

label var nmath "Math Score (in SD)"
label var nread "Read Score (in SD)"

label var ll1totlbinlo "PY Days $<$15C"
label var ll1totlbinup "PY Days $>$21C"

label var t_1 "NREGA: T = -3"
label var t_2 "NREGA: T = -2"
label var t_4 "NREGA: T = 0"
label var t_5 "NREGA: T = 1"
label var t_6 "NREGA: T = 2"
label var t_7 "NREGA: T = 3"
label var t_8 "NREGA: T = 4"
label var t_9 "NREGA: T = 5"
label var t_10 "NREGA: T = 6"
label var t_11 "NREGA: T = 7"

label var t_1_ll1totlbinup "NREGA: T = -3 * PY Days $>$21C"
label var t_2_ll1totlbinup "NREGA: T = -2 * PY Days $>$21C"
label var t_4_ll1totlbinup "NREGA: T = 0 * PY Days $>$21C"
label var t_5_ll1totlbinup "NREGA: T = 1 * PY Days $>$21C"
label var t_6_ll1totlbinup "NREGA: T = 2 * PY Days $>$21C"
label var t_7_ll1totlbinup "NREGA: T = 3 * PY Days $>$21C"
label var t_8_ll1totlbinup "NREGA: T = 4 * PY Days $>$21C"
label var t_9_ll1totlbinup "NREGA: T = 5 * PY Days $>$21C"
label var t_10_ll1totlbinup "NREGA: T = 6 * PY Days $>$21C"
label var t_11_ll1totlbinup "NREGA: T = 7 * PY Days $>$21C"

label var lnrega "NREGA PY"



*Figure 6 and Table 5

preserve
local tablename "T5"
local table=1
local col=1
estimates clear

foreach var of varlist nmath nread  {
areg `var' t_1 t_2 t_4 t_5 t_6 t_7 t_8 t_9 t_10 t_11 ll1totlbinlo ll1totlbinup ///
t_1_ll1totlbinup t_2_ll1totlbinup t_4_ll1totlbinup t_5_ll1totlbinup t_6_ll1totlbinup t_7_ll1totlbinup t_8_ll1totlbinup t_9_ll1totlbinup t_10_ll1totlbinup t_11_ll1totlbinup ///
t_1_ll1totlbinlo t_2_ll1totlbinlo t_4_ll1totlbinlo t_5_ll1totlbinlo t_6_ll1totlbinlo t_7_ll1totlbinlo t_8_ll1totlbinlo t_9_ll1totlbinlo t_10_ll1totlbinlo t_11_ll1totlbinlo ///
ll1lrain1 ll1lrain3 l1lhumid1 l1lhumid4 i.year i.childage, absorb(gdistid) cluster(gdistid)
estimates store cT`table'C`col++'

		   
foreach num of numlist 1 2 4 5 6 7 8 9 10 11 {
gen t`var'_`num' = _b[t_`num']
gen tcil`var'_`num' = _b[t_`num'] - 1.96*_se[t_`num']
gen tciu`var'_`num' = _b[t_`num'] + 1.96*_se[t_`num']

gen t2`var'_`num' = _b[t_`num'_ll1totlbinup]
gen t2cil`var'_`num' = _b[t_`num'_ll1totlbinup] - 1.96*_se[t_`num'_ll1totlbinup]
gen t2ciu`var'_`num' = _b[t_`num'_ll1totlbinup] + 1.96*_se[t_`num'_ll1totlbinup]
}
}


foreach var of varlist nmath nread     {
gen t`var' = .
gen tcil`var' = .
gen tciu`var' = .

gen t2`var' = .
gen t2cil`var' = .
gen t2ciu`var' = .

label var tcil`var' "95% CI"
label var tciu`var' "95% CI"

label var t2cil`var' "95% CI"
label var t2ciu`var' "95% CI"

foreach num of numlist 1 2 4 5 6 7 8 9 10 11 {
replace t`var' = t`var'_`num' if t_`num' == 1 
replace tcil`var' = tcil`var'_`num' if t_`num' == 1 
replace tciu`var' = tciu`var'_`num' if t_`num' == 1 

replace t2`var' = t2`var'_`num' if t_`num' == 1
replace t2cil`var' = t2cil`var'_`num' if t_`num' == 1 
replace t2ciu`var' = t2ciu`var'_`num' if t_`num' == 1 
}
}

replace tnmath = 0 if t_3 == 1
replace tcilnmath = 0 if t_3 == 1
replace tciunmath = 0 if t_3 == 1
replace tnread = 0 if t_3 == 1
replace tcilnread = 0 if t_3 == 1
replace tciunread = 0 if t_3 == 1

replace t2nmath = 0 if t_3 == 1
replace t2cilnmath = 0 if t_3 == 1
replace t2ciunmath = 0 if t_3 == 1
replace t2nread = 0 if t_3 == 1
replace t2cilnread = 0 if t_3 == 1
replace t2ciunread = 0 if t_3 == 1

foreach var of varlist nmath    {
twoway (connected t2`var' t2cil`var' t2ciu`var' t if t > -3,   sort xline(-1, lcolor(black)) yline(0, lcolor(black)) lcolor(red gray gray) mcolor(red gray gray) lpattern(solid dash dash) 	///
legend(order (3 1) label(3 "95% CI") label(1 "Estimated Coefficient")) xlabel(-2(1)7, labsize(medium)) ylabel(,  labsize(medium)) xtitle("NREGA (Previous Year)", size(medium)) ytitle("Math Score", size(medium)) ///
title("", size(medium) color(black)) graphregion(fcolor(white) color(white) icolor(white) margin(small)) plotregion(margin(vsmall))) 
graph export "$output/F6a.png", replace width(800) height(600) 
}

foreach var of varlist nread    {
twoway (connected t2`var' t2cil`var' t2ciu`var' t if t > -3,   sort xline(-1, lcolor(black)) yline(0, lcolor(black)) lcolor(red gray gray) mcolor(red gray gray) lpattern(solid dash dash) 	///
legend(order (3 1) label(3 "95% CI") label(1 "Estimated Coefficient")) xlabel(-2(1)7, labsize(medium)) ylabel(,  labsize(medium)) xtitle("NREGA (Previous Year)", size(medium)) ytitle("Read Score", size(medium)) ///
title("", size(medium) color(black)) graphregion(fcolor(white) color(white) icolor(white) margin(small)) plotregion(margin(vsmall))) 
graph export "$output/F6b.png", replace width(800) height(600) 
}

esttab cT`table'C* using "$output/`tablename'.tex", replace f ///
			   label booktabs b(3) p(3) eqlabels(none) alignment(S) collabels("\multicolumn{1}{c}{$\beta$ / SE}") ///
               keep (t_1 t_2 t_4 t_5 t_6 t_7 t_8 t_9 t_10 t_11 ll1totlbinlo ll1totlbinup t_1_ll1totlbinup t_2_ll1totlbinup t_4_ll1totlbinup t_5_ll1totlbinup t_6_ll1totlbinup ///
			   t_7_ll1totlbinup t_8_ll1totlbinup t_9_ll1totlbinup t_10_ll1totlbinup t_11_ll1totlbinup, relax) ///
			   star(* 0.10 ** 0.05 *** 0.01) ///
               cells("b(fmt(4)star)" "se(fmt(4)par)") ///
			   stats(N r2, fmt(0 3) layout("\multicolumn{1}{c}{@}") ///
			   labels(`"Observations"' `"\(R^{2}\)"'))				   
restore

